# Maintainer: Michael Hansen <zrax0111 gmail com>

_realname=capstone
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-capstone"
         "${MINGW_PACKAGE_PREFIX}-python-capstone")
_pkgver=4.0.2
pkgver=${_pkgver//-/}
pkgrel=1
pkgdesc='Lightweight multi-platform, multi-architecture disassembly framework'
url='https://www.capstone-engine.org/index.html'
arch=('any')
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=("${_realname}-${_pkgver}.tar.gz::https://github.com/aquynh/capstone/archive/${_pkgver}.tar.gz"
        "mingw_python.patch")
sha512sums=('7f93534517307b737422a8825b66b2a1f3e1cca2049465d60ab12595940154aaf843ba40ed348fce58de58b990c19a0caef289060eb72898cb008a88c470970e'
            'c86d1e9b14e848db1451a9f9422b3e6522b4a021f21a8bdd4b90d398f0e35423f019ec15ff7a0a6f8047c954e8651f57474112ce1afc8dc3f178221e51553e33')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}/mingw_python.patch"
}

build() {
  mkdir -p build-${MINGW_CHOST}
  cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_BUILD_TYPE=Release \
    ../${_realname}-${_pkgver}

  make
}

package_capstone() {
  cd build-${MINGW_CHOST}

  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/${_realname}-${_pkgver}/docs/README" -t "${pkgdir}/${MINGW_PREFIX}/share/doc/${_realname}"
  install -Dm644 "${srcdir}/${_realname}-${_pkgver}/LICENSE.TXT" -t "${pkgdir}/${MINGW_PREFIX}/share/licenses/${_realname}"
}

package_mingw-w64-i686-capstone()   { package_capstone; }
package_mingw-w64-x86_64-capstone() { package_capstone; }

package_python-capstone() {
  depends=("${MINGW_PACKAGE_PREFIX}-capstone"
           "${MINGW_PACKAGE_PREFIX}-python")
  provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
  replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
  cd ${_realname}-${_pkgver}/bindings/python

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  LIBCAPSTONE_PATH="${srcdir}/build-${MINGW_CHOST}/libcapstone.dll" \
  ${MINGW_PREFIX}/bin/python setup.py install -O1 --root="${pkgdir}" --prefix="${MINGW_PREFIX}"

  install -Dm644 "${srcdir}/${_realname}-${_pkgver}/LICENSE.TXT" -t "${pkgdir}/${MINGW_PREFIX}/share/licenses/python-${_realname}"
}

package_mingw-w64-i686-python-capstone()   { package_python-capstone; }
package_mingw-w64-x86_64-python-capstone() { package_python-capstone; }
